package com.guangfeng.yql.comm.util;

import java.util.HashMap;
import java.util.Map;

public class UriUtils {
    public static String getProperty(String uri, String key) {
        Map<String, String> map = getProperty2Map(uri);
        return map.get(key);
    }

    public static Map<String, String> getProperty2Map(String uri) {
        String propertyListStr = clearUri(uri);
        String[] split = propertyListStr.split("&&");
        HashMap<String, String> map = new HashMap<>();
        for (String kv : split) {
            if (!kv.contains("=")) {
                continue;
            }
            String[] kvList = new String[]{kv.substring(0, kv.indexOf("=")), kv.substring(kv.indexOf("=") + 1)};
            if (kvList[0].trim().equals("") || kvList[1].trim().equals("")) {
                continue;
            }
            map.put(kvList[0], kvList[1]);
        }
        return map;
    }

    private static String clearUri(String uri) {
        return uri.substring(uri.indexOf("?") + 1);
    }
}
